package com.java.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.java.model.Mail;
import com.java.model.PageBean;
import com.java.model.User;
import com.java.service.MailService;
import com.java.util.AppConfig;
import com.java.util.DateUtil;
import com.java.util.MailUtil;
import com.java.util.PageUtil;
import com.java.util.ResponseUtil;
import com.java.util.StringUtil;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/mail")
public class MailController {

	@Autowired
	private MailService mailService;

	/**
	 * 个人收件列表
	 * 
	 * @param page
	 * @param s_mail
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(value = "page", required = false) String page, Mail s_mail,
			HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		HttpSession session = request.getSession();
		System.out.println(page);
		if (StringUtil.isEmpty(page)) {
			page = "1";
			session.setAttribute("s_mail", s_mail);
			System.out.println(s_mail.getId());
		} else {
			s_mail = (Mail) session.getAttribute("s_mail");
		}
		// 过滤自己的邮件
		User currentUser = (User) session.getAttribute("currentUser");
		System.out.println("currentUser:" + currentUser.toString());
		s_mail.setToAddress(currentUser.getEmail());
		s_mail.setType("1");

		PageBean pageBean = new PageBean(Integer.parseInt(page), AppConfig.pageSize);
		List<Mail> mailList = mailService.find(pageBean, s_mail);
		int total = mailService.count(s_mail);
		String pageCode = PageUtil.getPagation(request.getContextPath() + "/mail/list.do", total,
				Integer.parseInt(page), AppConfig.pageSize);
		mav.addObject("pageCode", pageCode);

		mav.addObject("modeName", "收件箱");
		mav.addObject("mailList", mailList);
		mav.addObject("mainPage", "mail/list.jsp");
		mav.setViewName("main");
		return mav;
	}

	/**
	 * 已发邮件列表
	 * 
	 * @param page
	 * @param s_mail
	 * @param request
	 * @return
	 */
	@RequestMapping("/listf")
	public ModelAndView listf(@RequestParam(value = "page", required = false) String page, Mail s_mail,
			HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		HttpSession session = request.getSession();
		System.out.println(page);
		if (StringUtil.isEmpty(page)) {
			page = "1";
			session.setAttribute("s_mail", s_mail);
			System.out.println(s_mail.getId());
		} else {
			s_mail = (Mail) session.getAttribute("s_mail");
		}
		// 过滤自己的邮件
		User currentUser = (User) session.getAttribute("currentUser");
		s_mail.setFromAddress(currentUser.getEmail());
		s_mail.setType("1");

		PageBean pageBean = new PageBean(Integer.parseInt(page), AppConfig.pageSize);
		List<Mail> mailList = mailService.find(pageBean, s_mail);
		int total = mailService.count(s_mail);
		String pageCode = PageUtil.getPagation(request.getContextPath() + "/mail/list.do", total,
				Integer.parseInt(page), AppConfig.pageSize);
		mav.addObject("pageCode", pageCode);

		mav.addObject("modeName", "已发邮件列表");
		mav.addObject("mailList", mailList);
		mav.addObject("mainPage", "mail/list.jsp");
		mav.setViewName("main");
		return mav;
	}

	@RequestMapping("/preSave")
	public ModelAndView preSave(@RequestParam(value = "id", required = false) String id) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("mainPage", "mail/save.jsp");
		mav.addObject("modeName", "发送邮件");
		mav.setViewName("main");
		if (StringUtil.isNotEmpty(id)) {
			mav.addObject("actionName", "发送邮件");
			Mail mail = mailService.loadById(Integer.parseInt(id));
			mav.addObject("mail", mail);
		} else {
			mav.addObject("actionName", "发送邮件");
		}
		return mav;
	}

	@RequestMapping("/save")
	public String save(Mail mail) {
		if (mail.getId() == null) {
			if (mail.getSendTime() == null) {
				mail.setSendTime(DateUtil.formatDate(new Date(), "yyyy-MM-dd hh:mm:ss"));
			}
			// mailService.add(mail);
			try {
//				MailUtil.sendMail(AppConfig.smailName, AppConfig.rmailName, AppConfig.smailName, AppConfig.spasswd,
//						mail);
				MailUtil.sendMail(AppConfig.rmailName, AppConfig.smailName, AppConfig.rmailName, AppConfig.rpasswd,
						mail);
			} catch (Exception e) {
				System.out.println("发送邮件失败");
			}
		} else {
			mailService.update(mail);
		}
		return "redirect:/mail/listf.do";
	}

	@RequestMapping("/delete")
	public void delete(@RequestParam(value = "id") String id, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		mailService.delete(Integer.parseInt(id));
		result.put("success", true);
		ResponseUtil.write(result, response);
	}
}
